package _021_10_5

type TreeNode2 struct {
	Val   int
	Left  *TreeNode2
	Right *TreeNode2
}

/**
 *
 * @param root TreeNode类
 * @return int整型二维数组
 */
func levelOrder2(root *TreeNode2) [][]int {
	queue := make([]*TreeNode2, 0)
	queue = append(queue, root)
	for i := 0; i < len(queue); i++ {
		if queue[i].Left != nil {
			queue = append(queue, queue[i].Left)
		}
		if queue[i].Right != nil {
			queue = append(queue, queue[i].Right)
		}
	}
	return nil
}

func levelOrder(root *TreeNode2) [][]int {
	if root == nil {
		return nil
	}
	queue := make([]*TreeNode2, 0)
	queue = append(queue, root)
	result := make([][]int, 0)
	for {
		size := len(queue)
		if size <= 0 {
			break
		}
		var item []int
		for i := 0; i < size; i++ {
			item = append(item, queue[i].Val)
			if queue[i].Left != nil {
				queue = append(queue, queue[i].Left)
			}
			if queue[i].Right != nil {
				queue = append(queue, queue[i].Right)
			}
		}
		queue = queue[size:]
		result = append(result, item)
	}
	return result
}
